﻿using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using HML.Design;

namespace HML
{
    /// <summary>
    /// 面包屑导航选项
    /// </summary>
    [TypeConverter(typeof(PropertyOrderConverter))]
    public class BreadcrumbBarItem
    {
        #region 属性

        private StyleObject stateAppearance;
        /// <summary>
        /// 风格外观
        /// </summary>
        [Description("风格外观")]
        [PropertyOrder(-200)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public StyleObject StyleAppearance
        {
            get
            {
                if (this.stateAppearance == null)
                    this.stateAppearance = new StyleObject(this);
                return this.stateAppearance;
            }
        }

        private string text;
        /// <summary>
        /// 文本
        /// </summary>
        [Description("文本")]
        [PropertyOrder(-198)]
        [DefaultValue("")]
        public string Text
        {
            get { return this.text; }
            set
            {
                if (this.text == value)
                    return;

                this.text = value;
                this.InitializeInvalidate();
            }
        }

        private object tag = null;
        /// <summary>
        /// 自定义数据
        /// </summary>
        [Description("自定义数据")]
        [Category("杂项")]
        [PropertyOrder(-196)]
        [DefaultValue(null)]
        [Bindable(true)]
        [Localizable(false)]
        [TypeConverter(typeof(StringConverter))]
        public virtual object Tag
        {
            get { return this.tag; }
            set { this.tag = value; }
        }

        /// <summary>
        /// 鼠标是否已进入
        /// </summary>
        [Browsable(false)]
        public bool IsMouseEnter
        {
            get { return this.owner.IsMouseEnter(this); }
        }

        private BreadcrumbBar owner = null;
        /// <summary>
        /// 选项所属的控件
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public BreadcrumbBar Owner
        {
            get { return this.owner; }
            internal protected set { this.owner = value; }
        }

        #region 临时存储

        /// <summary>
        /// 选项背景RectF
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal RectangleF BackRectF { get; set; }

        /// <summary>
        /// 选项文字SizeF
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal SizeF TextSize { get; set; }

        /// <summary>
        /// 符号RectF
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal RectangleF SymbolRectF { get; set; }

        /// <summary>
        /// 不规则图形路径
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal GraphicsPath Path { get; set; }

        #endregion

        #endregion

        #region 私有方法

        /// <summary>
        /// 刷新控件
        /// </summary>
        private void Invalidate()
        {
            if (this.owner != null)
            {
                if (this.owner.Items.Contains(this))
                {
                    this.owner.Invalidate();
                }
                else
                {
                    this.owner = null;
                }
            }
        }

        /// <summary>
        /// 重新布局内部元素并刷新控件
        /// </summary>
        private void InitializeInvalidate()
        {
            if (this.owner != null)
            {
                if (this.owner.Items.Contains(this))
                {
                    this.owner.InitializeElement();
                    this.owner.Invalidate();
                }
                else
                {
                    this.owner = null;
                }
            }
        }

        #endregion

        #region 公开方法

        /// <summary>
        /// 复制选项
        /// </summary>
        /// <returns></returns>
        public virtual BreadcrumbBarItem Copy()
        {
            BreadcrumbBarItem item = new BreadcrumbBarItem() { Text = this.Text };
            item.StyleAppearance.UnSelected.Normal.BackColor = this.StyleAppearance.UnSelected.Normal.BackColor;
            item.StyleAppearance.UnSelected.Normal.TextColor = this.StyleAppearance.UnSelected.Normal.TextColor;
            item.StyleAppearance.UnSelected.Enter.BackColor = this.StyleAppearance.UnSelected.Enter.BackColor;
            item.StyleAppearance.UnSelected.Enter.TextColor = this.StyleAppearance.UnSelected.Enter.TextColor;
            item.StyleAppearance.UnSelected.Disabled.BackColor = this.StyleAppearance.UnSelected.Disabled.BackColor;
            item.StyleAppearance.UnSelected.Disabled.TextColor = this.StyleAppearance.UnSelected.Disabled.TextColor;
            item.StyleAppearance.Selected.Normal.BackColor = this.StyleAppearance.Selected.Normal.BackColor;
            item.StyleAppearance.Selected.Normal.TextColor = this.StyleAppearance.Selected.Normal.TextColor;
            item.StyleAppearance.Selected.Enter.BackColor = this.StyleAppearance.Selected.Enter.BackColor;
            item.StyleAppearance.Selected.Enter.TextColor = this.StyleAppearance.Selected.Enter.TextColor;
            item.StyleAppearance.Selected.Disabled.BackColor = this.StyleAppearance.Selected.Disabled.BackColor;
            item.StyleAppearance.Selected.Disabled.TextColor = this.StyleAppearance.Selected.Disabled.TextColor;

            return item;
        }

        #endregion

        [TypeConverter(typeof(PropertyOrderConverter))]
        public class StyleObject
        {
            private BreadcrumbBarItem owner = null;
            internal StyleObject(BreadcrumbBarItem owner) { this.owner = owner; }

            #region 属性

            private StateObject unSelected;
            /// <summary>
            /// 未选中状态外观
            /// </summary>
            [Description("未选中状态外观")]
            [PropertyOrder(-178)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public StateObject UnSelected
            {
                get
                {
                    if (this.unSelected == null)
                        this.unSelected = new StateObject(this);
                    return this.unSelected;
                }
            }

            private StateObject selected;
            /// <summary>
            /// 已选中状态外观
            /// </summary>
            [Description("已选中状态外观")]
            [PropertyOrder(-176)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public StateObject Selected
            {
                get
                {
                    if (this.selected == null)
                        this.selected = new StateObject(this);
                    return this.selected;
                }
            }

            #endregion

            [TypeConverter(typeof(PropertyOrderConverter))]
            public class StateObject
            {
                private StyleObject owner = null;
                internal StateObject(StyleObject owner) { this.owner = owner; }

                #region 属性

                private ItemObject normal;
                /// <summary>
                /// 正常状态
                /// </summary>
                [Description("正常状态")]
                [PropertyOrder(-194)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                public ItemObject Normal
                {
                    get
                    {
                        if (this.normal == null)
                            this.normal = new ItemObject(this);
                        return this.normal;
                    }
                }

                private ItemObject enter;
                /// <summary>
                /// 鼠标进入状态
                /// </summary>
                [Description("鼠标进入状态")]
                [PropertyOrder(-192)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                public ItemObject Enter
                {
                    get
                    {
                        if (this.enter == null)
                            this.enter = new ItemObject(this);
                        return this.enter;
                    }
                }

                private ItemObject disabled;
                /// <summary>
                /// 禁用状态
                /// </summary>
                [Description("禁用状态")]
                [PropertyOrder(-190)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                public ItemObject Disabled
                {
                    get
                    {
                        if (this.disabled == null)
                            this.disabled = new ItemObject(this);
                        return this.disabled;
                    }
                }

                #endregion

                [TypeConverter(typeof(PropertyOrderConverter))]
                public class ItemObject
                {
                    private StateObject owner = null;
                    internal ItemObject(StateObject owner) { this.owner = owner; }

                    #region 私有方法

                    /// <summary>
                    /// 刷新控件
                    /// </summary>
                    private void Invalidate()
                    {
                        if (this.owner != null && this.owner.owner != null && this.owner.owner != null && this.owner.owner.owner.owner != null)
                        {
                            BreadcrumbBar breadcrumbBar = (BreadcrumbBar)this.owner.owner.owner.owner;
                            if (breadcrumbBar.Items.Contains(this))
                            {
                                breadcrumbBar.Invalidate();
                            }
                            else
                            {
                                this.owner = null;
                            }
                        }
                    }

                    /// <summary>
                    /// 重新布局内部元素并刷新控件
                    /// </summary>
                    private void InitializeInvalidate()
                    {
                        if (this.owner != null && this.owner.owner != null && this.owner.owner != null && this.owner.owner.owner.owner != null)
                        {
                            BreadcrumbBar breadcrumbBar = (BreadcrumbBar)this.owner.owner.owner.owner;
                            if (breadcrumbBar.Items.Contains(this))
                            {
                                breadcrumbBar.InitializeElement();
                                breadcrumbBar.Invalidate();
                            }
                            else
                            {
                                this.owner = null;
                            }
                        }
                    }

                    #endregion

                    #region 属性

                    private Color backColor = Color.Empty;
                    /// <summary>
                    /// 背景颜色
                    /// </summary>
                    [Description("背景颜色")]
                    [PropertyOrder(-158)]
                    [DefaultValue(typeof(Color), "")]
                    [Editor(typeof(AlphaColorEditor), typeof(UITypeEditor))]
                    public Color BackColor
                    {
                        get { return this.backColor; }
                        set
                        {
                            if (this.backColor != value)
                            {
                                this.backColor = value;
                                this.Invalidate();
                            }
                        }
                    }

                    private Color textColor = Color.Empty;
                    /// <summary>
                    /// 文本颜色
                    /// </summary>
                    [Description("文本颜色")]
                    [PropertyOrder(-157)]
                    [DefaultValue(typeof(Color), "")]
                    [Editor(typeof(AlphaColorEditor), typeof(UITypeEditor))]
                    public Color TextColor
                    {
                        get { return this.textColor; }
                        set
                        {
                            if (this.textColor == value)
                                return;

                            this.textColor = value;
                            this.Invalidate();
                        }
                    }

                    #endregion

                }

            }

        }

    }

}
